#include "lora_thread.h"
#include "main.h"
#include "gpio.h"
#include "spi.h"
#include "user_led.h"
#include "user_usart.h"
#include "Model_Info.h"
#include "radio.h"	
#include "Switch.h"
lora_t lora_sx126;

rt_mq_t  lora_send_mq=RT_NULL;         //lora发送消息队列

extern Mallco_dev mallco_dev;         //内存管理器
  
rt_sem_t Dio1_EXIT_sem;               //DIO0中断信号量
uint8_t tx_model=0;

void Lora_Recive(uint8_t *data,uint8_t len);
void Lora_Relay(LoRa_LLP_Hand_t hand,uint8_t *data,uint8_t len);    //中继
void Replay_LoRaSend_Data(LoRa_LLP_Hand_t hand,uint8_t *data,uint8_t len);  //中继发送数据
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin==RA_D1_Pin)
	{
		if(tx_model==1)
		{
			rt_sem_release(Dio1_EXIT_sem);
		}
	}
}
/************************************
 * 功能：GPIO 端口函数及延时函数
 * 参数：
 * 返回：
 * *********************************/
uint32_t nf_gpio_and_delay(lora_t *nf,uint8_t msg, uint8_t arg_int,void *arg_ptr,void *res_arg_ptr)
{
	uint32_t status=0;
	uint32_t udelay= *(uint32_t *)arg_ptr;
	switch (msg)
	{
		case LoRa_MSG_DELAY_MILLI:
			rt_thread_mdelay(arg_int);
			break;
		case LoRa_MSG_GPIO_CS:              //CS引脚
			if(arg_int)
			{
				HAL_GPIO_WritePin(RA_CSN_GPIO_Port,RA_CSN_Pin,GPIO_PIN_SET);
			}
			else
			{
				HAL_GPIO_WritePin(RA_CSN_GPIO_Port,RA_CSN_Pin,GPIO_PIN_RESET);
			}
			break;
		case LoRa_MSG_GPIO_RST:
			if(arg_int)
			{
				HAL_GPIO_WritePin(RA_RST_GPIO_Port,RA_RST_Pin,GPIO_PIN_SET);
			}
			else
			{
				HAL_GPIO_WritePin(RA_RST_GPIO_Port,RA_RST_Pin,GPIO_PIN_RESET);
			}
			break;
		case LoRa_MSG_GPIO_ReadDio0:    //读取DIO0 状态
			//status=HAL_GPIO_ReadPin(RA_DIO0_GPIO_Port,RA_DIO0_Pin);
		  break;
		case LoRa_MSG_GPIO_ReadDio1:   //读取DIO1 状态
			status=HAL_GPIO_ReadPin(RA_D1_GPIO_Port,RA_D1_Pin);
		  break;
		case LoRa_MSG_GPIO_BUSY:
			status=HAL_GPIO_ReadPin(RA_BUSY_GPIO_Port,RA_BUSY_Pin);
		  break;
		case LoRa_MSG_GetTick:        //获取系统时间
			  status=HAL_GetTick();
		  break;
		case LoRa_MSG_GPIO_EXIT_DIO1:  //等待DIO0 中断
			   tx_model=1;
			   status=rt_sem_take(Dio1_EXIT_sem, udelay);
		     tx_model=0;
			   break;
		default:
			break;
	}
	return status;
}
/*******************************************
 * 功能：spi通信函数
 * 参数：
 * 返回：
 * ****************************************/
uint32_t  lora_4wire_hw_spi(lora_t *nf, uint8_t msg, uint8_t arg_int,void *arg_ptr,void *res_arg_ptr)
{
	switch(msg)
	{
		case LoRa_MSG_BYTE_SEND:
			HAL_SPI_TransmitReceive(&hspi2,(uint8_t *)arg_ptr,(uint8_t *)res_arg_ptr,arg_int,1000);
//			HAL_SPI_TransmitReceive_DMA(&hspi1,(uint8_t *)arg_ptr,(uint8_t *)res_arg_ptr,arg_int);
			break;
		case LoRa_MSG_BYTE_START_TRANSFER:
			break;
		case LoRa_MSG_BYTE_END_TRANSFER:
			break;
		case LoRa_MSG_BYTE_INIT:
			break;
		case LoRa_MSG_BYTE_Transmit:
			HAL_SPI_Transmit(&hspi2,(uint8_t *)arg_ptr,arg_int,1000);
		    break;
		case LoRa_MSG_BYTE_Receive:
		    HAL_SPI_Receive(&hspi2,(uint8_t *)arg_ptr,arg_int,1000);
		    break;
		default:
			return 0;
	}
	return 1;
}

//#define TX_MOD 1
lora_config_t localSettingSave = {435000000, 20, 7, 7, 1, 0x000f}; // 本地保存的配置信息
uint8_t rx_buf[128];
uint8_t rx_len=128;
/// @brief lora 任务入口函数
/// @param paremeter 
void lora_thread_entry(void *paremeter)
{
	uint32_t rf=0;
	uint8_t rx_status=0;
	LoRaData_t LoraSend_data;
	lora_send_mq=rt_mq_create("lora_send_mq",      //消息队列名称
	                              sizeof(LoRaData_t),                   //消息最大长度
	                              10,                   //队列长度
	                             RT_IPC_FLAG_FIFO      //等待的方式
	                             );
	Dio1_EXIT_sem = rt_sem_create("DIO1_EXIT", 0, RT_IPC_FLAG_PRIO);     //创建串口数据接收信号量
	Lora_Setup_SX126x_noname_f(&lora_sx126,lora_4wire_hw_spi,nf_gpio_and_delay);
//	localSettingSave.Power=model_info_handle.model_info.rf_config.RF_power;
//	localSettingSave.RFFrequency=model_info_handle.model_info.rf_config.RF_TXFrequency;
//	localSettingSave.SignalBw=model_info_handle.model_info.rf_config.SignalBw;
//	localSettingSave.SpreadingFactor=model_info_handle.model_info.rf_config.SpreadingFactor;
//	localSettingSave.ErrorCoding=model_info_handle.model_info.rf_config.ErrorCoding;
	
  rf=420000000+(model_info_handle.general_info.SN%30)*1000000;
																
	localSettingSave.Power=model_info_handle.model_info.rf_config.RF_power;
	localSettingSave.RFFrequency=rf;
	localSettingSave.SignalBw=model_info_handle.model_info.rf_config.SignalBw;
	localSettingSave.SpreadingFactor=model_info_handle.model_info.rf_config.SpreadingFactor;
	localSettingSave.ErrorCoding=model_info_handle.model_info.rf_config.ErrorCoding;
																
  sx126xLoraInit(&lora_sx126, &localSettingSave);
	//RadioSetChannel(&lora,)
	//sx127xInit(&lora_sx127, &localSettingSave);
	//sx127xStartRx(&lora_sx127,model_info_handle.model_info.rf_config.RF_RXFrequency);
	sx126xStartRx(&lora_sx126,model_info_handle.model_info.rf_config.RF_RXFrequency);														
  while(1)
	{
		if(rt_mq_recv(lora_send_mq,&LoraSend_data,sizeof(LoRaData_t),0) == RT_EOK)
		{
			if(sx126xSend(&lora_sx126,model_info_handle.model_info.rf_config.RF_TXFrequency,LoraSend_data.data,LoraSend_data.len,2000)==0)
			{
			}
			sx126xStartRx(&lora_sx126,model_info_handle.model_info.rf_config.RF_RXFrequency);
			myfree(&mallco_dev,LoraSend_data.data);
		}
		rx_len=128;
		rx_status=sx126xRxData(&lora_sx126,rx_buf,&rx_len);
		if(rx_status==0)
		{
			Lora_Recive(rx_buf,rx_len);
		}
		rt_thread_mdelay(20);
	}
}
/**********************************/
void Lora_Recive(uint8_t *data,uint8_t len)
{
	Lora_Switch_Recive(data,len);
}
/*************************
 * 功能：Lora数据中继
 * 参数：
 *        hand：数据包头
 *        data：数据
 *        len：数据长度
*************************/
void Lora_Relay(LoRa_LLP_Hand_t hand,uint8_t *data,uint8_t len)    //中继
{
	uint8_t relay_id1=(uint8_t)model_info_handle.model_info.net_config.relay_id1;
	uint8_t relay_id2=(uint8_t)model_info_handle.model_info.net_config.relay_id2;
	if((hand.net_id==relay_id1)||(hand.net_id==relay_id2))
	{
		//LED_Link();
		if(hand.net_id==relay_id1)
		{
			hand.net_id=relay_id2;
		}
		else
		{
			hand.net_id=relay_id1;
		}
		Replay_LoRaSend_Data(hand,data,len);
	}
}
void Replay_LoRaSend_Data(LoRa_LLP_Hand_t hand,uint8_t *data,uint8_t len)
{
	LoRaData_t send_data;
	uint8_t hand_len=sizeof(LoRa_LLP_Hand_t);   //包头长度
	//hand.net_id=model_info_handle.model_info.net_config.id;
	//hand.source_add=model_info_handle.model_info.net_config.add;
	send_data.data=mymalloc(&mallco_dev,hand_len+len);
	if(send_data.data)
	{
		send_data.len=len+hand_len;
		mymemcpy(send_data.data,&hand,hand_len);    //复制包头
		mymemcpy(send_data.data+hand_len,data,len);
	  if(rt_mq_send(lora_send_mq,&send_data,sizeof(LoRaData_t))!=RT_EOK)
		{
			myfree(&mallco_dev,send_data.data);
		}
	}
}
/********************
 * 功能：用户通过Lora发送数据
 * 参数：
********************/
void User_LoRaSend_Data(uint8_t *data,uint8_t len)
{
	LoRaData_t lora_data;  
	lora_data.len=len;
  lora_data.data=mymalloc(&mallco_dev,len);
	if(lora_data.data)
	{
		mymemcpy(lora_data.data,data,len);    //复制包头
	  if(rt_mq_send(lora_send_mq,&lora_data,sizeof(LoRaData_t))!=RT_EOK)
		{
			myfree(&mallco_dev,lora_data.data);
		}
	}
}
